import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.ButtonGroup;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import javax.swing.JTextArea;
import javax.swing.JToolBar;
import org.jhotdraw.framework.*;
import org.jhotdraw.standard.*;
import org.jhotdraw.util.UndoableTool;
import org.jhotdraw.figures.*;
import org.jhotdraw.application.DrawApplication;

/**
 * @version 17
 */

public class MainWindow extends DrawApplication implements ActionListener {
	/*
	 * MEMBER VARIABLES
	 */

	public static String alpha_var, stackalpha_var, word_var, setOfStates_var,
			StartState_var, AcceptStates_var, Transitions_var, Type_of_D = "";
	public static ButtonGroup buttonGroup;
	public static JRadioButton DFA_rb, NFA_rb, PDA_rb;
	public static MainWindow _window;
	public static boolean initial = true;
	public static JTextArea alpha_tbox = new JTextArea("", 8, 0);
	public static JTextArea word_tbox = new JTextArea("", 8, 0);
	public static JTextArea setOfStates_tbox = new JTextArea("", 8, 0);
	public static JTextArea StartState_tbox = new JTextArea("", 8, 0);
	public static JTextArea AcceptStates_tbox = new JTextArea("", 8, 0);
	public static JTextArea Transitions_tbox = new JTextArea("", 8, 0);
	public static JTextArea StackAlpha_tbox = new JTextArea("", 8, 0);
	private ProxyDiagram _pd;
	private static JTextField StackAlpha_lb = new JTextField();
	private JToolBar newToolBar = this.createTools();
	private JToolBar newnewToolBar = this.createMoreTools();

	/*
	 * CONSTRUCTORS
	 */

	public MainWindow() {
		super("Finite Automata Simulating and Diagramming Tool");
		alpha_tbox.setLineWrap(true);
		word_tbox.setLineWrap(true);
		setOfStates_tbox.setLineWrap(true);
		StartState_tbox.setLineWrap(true);
		AcceptStates_tbox.setLineWrap(true);
		Transitions_tbox.setLineWrap(true);
		StackAlpha_tbox.setLineWrap(true);
		alpha_tbox
				.setToolTipText("Enter the letters in the alphabet of this automata, without spaces, separated by commas.  ");
		word_tbox
				.setToolTipText("Enter the word you want to test.  Make sure all of its letters are in the alphabet!  ");
		setOfStates_tbox
				.setToolTipText("Enter the name of the states of this automata, without spaces, separated by commas.  If you want state q1 to appear at coordinates x,y, type q1[x]y.  Otherwise, it will be given a default position.  ");
		StartState_tbox
				.setToolTipText("Enter only the name of the start state.  ");
		AcceptStates_tbox
				.setToolTipText("Enter the names of the accept states, without spaces, separated by commas.  ");
		Transitions_tbox
				.setToolTipText("Enter each transition, without spaces, separated by commas.  To indicate a transition from state q1 to state q2 after reading the letter 0, type q1+0=q2.  To indicate an epsilon transitoin, type q1+=q2.  ");
		StackAlpha_tbox
				.setToolTipText("Enter the tape alphabet for your PDA, without spaces, separated by commas.  ");
	}

	/*
	 * CONCRETE METHODS
	 */

	public static void writeSaveFile() {
		String newline = System.getProperty("line.separator");
		String[] data = MainWindow._window._pd.writeSave().split(";");
		String file = "States:  " + data[1] + newline + newline;
		file += "Alphabet:  " + data[0] + newline + newline;
		file += "Start state:  " + data[2] + newline + newline;
		file += "Accept states:  " + data[3] + newline + newline;
		file += "Transitions:  " + data[4];
		BufferedWriter writer = null;
		try {
			String path = MainWindow.class.getProtectionDomain()
					.getCodeSource().getLocation().getPath();
			writer = new BufferedWriter(new FileWriter(path
					+ "_saved inputs.txt"));
			// writer.write(MainWindow._window._pd.writeSave());
			writer.write(file);
		} catch (IOException e) {
			System.err.println(e);
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					System.err.println(e);
				}
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("PDA")) {
			MainWindow.StackAlpha_lb.setVisible(true);
			MainWindow.StackAlpha_tbox.setVisible(true);
			Type_of_D = "PDA";
		} else if (e.getActionCommand().equals("DFA")) {
			MainWindow.StackAlpha_lb.setVisible(false);
			MainWindow.StackAlpha_tbox.setVisible(false);
			Type_of_D = "DFA";
		} else if (e.getActionCommand().equals("NFA")) {
			MainWindow.StackAlpha_lb.setVisible(false);
			MainWindow.StackAlpha_tbox.setVisible(false);
			MainWindow.Type_of_D = "NFA";
		}

	}

	public JToolBar createTools() {
		// This code makes the WEST toolbar.
		

		JToolBar tools = createToolPalette();
		tools.setSize(100, 300);
		
		JTextField setOfStates_lb = new JTextField("Enter all states: ");
		setOfStates_lb.setEditable(false);
		tools.add(setOfStates_lb);
		tools.add(setOfStates_tbox);

		JTextField enter_alpha = new JTextField("Enter Alphabet: ");
		enter_alpha.setEditable(false);
		tools.add(enter_alpha);
		tools.add(alpha_tbox);
		
		JTextField StartState_lb = new JTextField("Enter start state: ");
		StartState_lb.setEditable(false);
		tools.add(StartState_lb);
		tools.add(StartState_tbox);

		JTextField AcceptStates_lb = new JTextField("Enter accept states: ");
		AcceptStates_lb.setEditable(false);
		tools.add(AcceptStates_lb);
		tools.add(AcceptStates_tbox);

		JTextField Transitions_lb = new JTextField("Enter transitions: ");
		Transitions_lb.setEditable(false);
		tools.add(Transitions_lb);
		tools.add(Transitions_tbox);

		JTextField enter_word = new JTextField("Enter Word: ");
		enter_word.setEditable(false);
		tools.add(enter_word);
		tools.add(word_tbox);
		
		StackAlpha_lb.setText("Enter Stack Alphabet: ");
		StackAlpha_lb.setEditable(false);
		tools.add(StackAlpha_lb);

		tools.add(StackAlpha_tbox);
		MainWindow.StackAlpha_lb.setVisible(false);
		MainWindow.StackAlpha_tbox.setVisible(false);

		return tools;
	}

	public JToolBar createMoreTools() {
		// This code makes the EAST toolbar.
		JToolBar moretools = createToolPalette();

		Check_Word_Button checkWord_bt = new Check_Word_Button("Check word");
		checkWord_bt.setBackground(Color.GREEN);
		moretools.add(checkWord_bt);

		DrawFigure_Button DrawFigure_bt = new DrawFigure_Button("Draw "
				+ MainWindow.Type_of_D);
		DrawFigure_bt.setBackground(Color.RED);
		moretools.add(DrawFigure_bt);

		Save_Button save_bt = new Save_Button("Save input");
		save_bt.setBackground(Color.ORANGE);
		moretools.add(save_bt);

		return moretools;
	}

	public void createTools(JToolBar palette) {
		// This code makes the TOP toolbar.
		super.createTools(palette);

		Tool tool = new UndoableTool(new TextTool(this, new TextFigure()));
		palette.add(createToolButton(IMAGES + "TEXT",
				"Rename a state or transition", tool));

		StateFigure start = new StateFigure();
		start.setIsStartState(true);
		tool = new CreationTool(this, start);
		palette.add(createToolButton(IMAGES + "STARTS", "Create a start state",
				tool));

		tool = new CreationTool(this, new StateFigure());
		palette.add(createToolButton(IMAGES + "ELLIPSE", "Create a state", tool));

		StateFigure start_accept = new StateFigure();
		start_accept.setIsStartState(true);
		start_accept.setIsAcceptState(true);
		tool = new CreationTool(this, start_accept);
		palette.add(createToolButton(IMAGES + "STARTACCEPTS",
				"Create a start/accept state", tool));

		StateFigure accept = new StateFigure();
		accept.setIsAcceptState(true);
		tool = new CreationTool(this, accept);
		palette.add(createToolButton(IMAGES + "ACCEPTS",
				"Create an accept state", tool));

		tool = new ConnectionTool(this, new Transitions());
		palette.add(createToolButton(IMAGES + "TRANS",
				"Create a transition between two states", tool));

		tool = new UndoableTool(new ConnectedTextTool(this, new TextFigure()));
		palette.add(createToolButton(IMAGES + "ATEXT", "Name a transition",
				tool));

		MainWindow.DFA_rb = new JRadioButton("Deterministic Finite Automata");
		MainWindow.DFA_rb.setActionCommand("DFA");
		MainWindow.NFA_rb = new JRadioButton(
				"Non-deterministic Finite Automata");
		MainWindow.NFA_rb.setActionCommand("NFA");
		MainWindow.PDA_rb = new JRadioButton("Push Down Automata");
		MainWindow.PDA_rb.setActionCommand("PDA");
		MainWindow.PDA_rb.addActionListener(MainWindow._window);
		MainWindow.NFA_rb.addActionListener(MainWindow._window);
		MainWindow.DFA_rb.addActionListener(MainWindow._window);

		ButtonGroup i = new ButtonGroup();
		i.add(MainWindow.DFA_rb);
		i.add(MainWindow.NFA_rb);
		i.add(MainWindow.PDA_rb);
		MainWindow.buttonGroup = i;
		palette.add(MainWindow.DFA_rb);
		palette.add(MainWindow.NFA_rb);
		palette.add(MainWindow.PDA_rb);

		

	}

	@Override
	protected void open(final DrawingView newDrawingView) {
		/* Adds the EAST & WEST tool bar */
		super.open(newDrawingView);

		JPanel jp = (JPanel) this.getContentPane().getComponents()[1];

		newToolBar.setOrientation(JToolBar.VERTICAL);
		newToolBar.setPreferredSize(new Dimension(160, 400));
		jp.add(newnewToolBar, BorderLayout.SOUTH);
		jp.add(newToolBar, BorderLayout.WEST);
		
		

	}

	/*
	 * STATIC METHODS
	 */

	public static boolean validityChecker() {
		String type = MainWindow.buttonGroup.getSelection().getActionCommand();
		// The below strings intentionally separate from the static global ones.
		// We don't want to update the global ones if this isn't valid.
		String alpha_var = alpha_tbox.getText();
		String word_var = word_tbox.getText();
		String setOfStates_var = setOfStates_tbox.getText();
		String StartState_var = StartState_tbox.getText();
		String AcceptStates_var = AcceptStates_tbox.getText();
		String Transitions_var = Transitions_tbox.getText();
		/*
		 * Currently checks if... The alphabet has reserved characters The state
		 * names have reserved characters (Note: Should not have to check others
		 * for reserved characters. If they're used, they'll get picked up by
		 * other errors) There are multiple copies of any letters There are
		 * multiple copies of any states If the start state is in the list of
		 * states If all the accepting states are in the list of states If any
		 * accepting states are repeated If transitions are incorrectly
		 * formatted If any of the states used in transitions are not defined If
		 * a DFA has an epsilon transition If the letter in a transition is in
		 * the alphabet Still needs to check if a DFA has multiple transitions
		 * for a single letter Also needs a bunch of checks related to PDAs, but
		 * these should wait until PDAs are functional
		 */
		boolean canContinue = true;
		String s1 = MainWindow
				.checkForForbiddenCharacters(alpha_var.split(","));
		if (!s1.equals("VALID")) { // Check if alphabet contains reserved
									// characters
			canContinue = false;
			String string = "Cannot contain character \"" + s1
					+ "\" in your alphabet.";
			JOptionPane.showMessageDialog(null, string, "Error!",
					JOptionPane.DEFAULT_OPTION);
		}
		String[] rawStates = setOfStates_var.split(",");
		for (int q = 0; q < rawStates.length; q++)
		{
			rawStates[q] = rawStates[q].split("\\[")[0];
		}
		s1 = MainWindow.checkForForbiddenCharacters(rawStates);
		if (!s1.equals("VALID")) {
			/* Check of state names contain forbidden characters */
			canContinue = false;
			String string = "Cannot contain character \"" + s1
					+ "\" in your state names.";
			JOptionPane.showMessageDialog(null, string, "Error!",
					JOptionPane.DEFAULT_OPTION);
		}
		for (String letter : alpha_var.split(",")) {
			if (MainWindow.checkIfAHasB(alpha_var.split(","), letter) != 1) {
				canContinue = false;
				String string = "You have multiple copies of letter \""
						+ letter + "\".";
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
		}
		for (String state : rawStates) {
			/*
			 * Check if there are multiple copies of any states
			 */
			if (MainWindow.checkIfAHasB(rawStates, state) > 1) {
				canContinue = false;
				String string = "You have multiple copies of state \"" + state
						+ "\".";
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
		}
		if (MainWindow.checkIfAHasB(rawStates, StartState_var) == 0) {
			// Check
			/*
			 * if the start state is in the list of states
			 */

			canContinue = false;
			String string = "Your start state is not in your list of states.";
			JOptionPane.showMessageDialog(null, string, "Error!",
					JOptionPane.DEFAULT_OPTION);
		}
		for (String state : AcceptStates_var.split(",")) {
			/*
			 * Check if any accepting states are not in the list of states
			 */

			if (MainWindow.checkIfAHasB(rawStates, state) == 0) {
				canContinue = false;
				String string = "The accepting state \"" + state
						+ "\" is not defined in your list of states!";
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
		}
		for (String state : AcceptStates_var.split(",")) {
			/*
			 * Check if any accepting states are repeated
			 */
			if (MainWindow.checkIfAHasB(AcceptStates_var.split(","), state) != 1) {
				canContinue = false;
				String string = "You have multiple copies of state \"" + state
						+ "\" in your list of accepting states!";
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
		}
		String[] transitions = Transitions_var.split(",");
		for (String trans : transitions) {
			if (MainWindow.checkIfAHasB(trans.split(""), "=") != 1
					|| MainWindow.checkIfAHasB(trans.split(""), "+") != 1) {
				/*
				 * Check if any transitions have more or less than one + or =.
				 */
				canContinue = false;
				String string = "Your transitions are formatted incorrectly!  Remember, if you want state s1 to ";
				string += "transition to state s2 when it reads the letter 0, type \"s1+0=s2\", without any spaces!  ";
				if (type.equals("NFA") || type.equals("PDA")) {
					string += "If you want s1 to transition to s2 via an epsilon transition, type \"s1+=s2\"!";
				}
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
			String[] trans2 = trans.split("=");
			String[] trans3 = trans2[0].split("\\+");
			if (MainWindow.checkIfAHasB(rawStates, trans2[1]) != 1) {
				/*
				 * Check if state after the = is in list of states
				 */
				canContinue = false;
				String string = "You never defined state \"" + trans2[1]
						+ "\", which you used in a transition.";
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
			if (MainWindow.checkIfAHasB(rawStates, trans3[0]) != 1) {
				/*
				 * Check if state before + is in list of states
				 */
				canContinue = false;
				String string = "You never defined state \"" + trans3[0]
						+ "\", which you used in a transition.";
				JOptionPane.showMessageDialog(null, string, "Error!",
						JOptionPane.DEFAULT_OPTION);
			}
			if (type.equals("DFA")) {
				if (trans3.length == 1) {
					/*
					 * Check if this is an epsilon transition in a DFA
					 */
					canContinue = false;
					String string = "You can't have epsilon transitions in a Deterministic Finite Automata!";
					JOptionPane.showMessageDialog(null, string, "Error!",
							JOptionPane.DEFAULT_OPTION);
					break;
				}
				/*
				 * Enter code here to check if any states have multiple
				 * transitions for single letters
				 */
			}
			if (trans3.length == 2) {
				if (MainWindow.checkIfAHasB(alpha_var.split(","), trans3[1]) != 1) {
					/*
					 * Check if the letter after the + is in the alphabet
					 */
					canContinue = false;
					String string = "The letter \"" + trans3[1]
							+ "\" isn't defined in your alphabet!";
					JOptionPane.showMessageDialog(null, string, "Error!",
							JOptionPane.DEFAULT_OPTION);
				}
			}
		}
		return canContinue;
	}

	public static String checkForForbiddenCharacters(String[] word) {
		for (int i = 1; i < word.length; i++) {
			if (word[i].equals(";")) {
				return ";";
			}
			if (word[i].equals("")) {
				return ",";
			}
			if (word[i].equals("=")) {
				return "=";
			}
			if (word[i].equals("+")) {
				return "+";
			}
			if (word[i].equals("[")) {
				return "[";
			}
			if (word[i].equals("]")) {
				return "]";
			}
			if (word[i].equals("\\")) {
				return "\\";
			}
			if (word[i].equals(" ")) {
				return " ";
			}
		}
		return "VALID";
	}

	// Returns the number of instances of b in A
	public static int checkIfAHasB(String[] A, String b) {
		int num = 0;
		for (String a : A) {
			if (a.equals(b)) {
				num++;
			}
		}
		return num;
	}

	/*
	 * GETTERS AND SETTERS
	 */

	public void setPD(ProxyDiagram pd) {
		this._pd = pd;
	}

	/*
	 * OTHER CLASSES
	 */

	public class DrawFigure_Button extends JButton {
		public DrawFigure_Button(String buttonText) {
			// super(buttonText);
			super.setText("Draw " + MainWindow.Type_of_D);
			this.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {

					if (MainWindow.validityChecker()) {
						/*
						 * Only do things if it's valid
						 */String type = MainWindow.buttonGroup.getSelection()
								.getActionCommand();
						alpha_var = alpha_tbox.getText();
						word_var = word_tbox.getText();
						setOfStates_var = setOfStates_tbox.getText();
						StartState_var = StartState_tbox.getText();
						AcceptStates_var = AcceptStates_tbox.getText();
						Transitions_var = Transitions_tbox.getText();
						if (type.equals("PDA")) {
							stackalpha_var = StackAlpha_tbox.getText();
						}
						MainWindow window = MainWindow._window;
						MainWindow.main(new String[] { alpha_var, "",
								setOfStates_var, StartState_var,
								AcceptStates_var, Transitions_var, type,
								stackalpha_var });
						window.exit();
					}
				}
			});
		}
	}

	public class Check_Word_Button extends JButton {
		public Check_Word_Button(String buttonText) {
			super(buttonText);
			this.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					word_var = word_tbox.getText();
					String type = MainWindow.buttonGroup.getSelection()
							.getActionCommand();
					String alpha_var1 = alpha_tbox.getText();
					String setOfStates_var1 = setOfStates_tbox.getText();
					String StartState_var1 = StartState_tbox.getText();
					String AcceptStates_var1 = AcceptStates_tbox.getText();
					String Transitions_var1 = Transitions_tbox.getText();
					boolean redrawn = false;
					if (!alpha_var1.equals(alpha_var)
							|| !setOfStates_var1.equals(setOfStates_var)
							|| !StartState_var1.equals(StartState_var)
							|| !AcceptStates_var1.equals(AcceptStates_var)
							|| !Transitions_var1.equals(Transitions_var)) {
						/*
						 * Redraw before simulating
						 */
						redrawn = true;
						if (MainWindow.validityChecker()) {
							/*
							 * Only do things if it's valid
							 */alpha_var = alpha_tbox.getText();
							word_var = word_tbox.getText();
							setOfStates_var = setOfStates_tbox.getText();
							StartState_var = StartState_tbox.getText();
							AcceptStates_var = AcceptStates_tbox.getText();
							Transitions_var = Transitions_tbox.getText();
							MainWindow window = MainWindow._window;
							MainWindow.main(new String[] { alpha_var, word_var,
									setOfStates_var, StartState_var,
									AcceptStates_var, Transitions_var, type });
							window.exit();
						}
					}
					if (!redrawn && MainWindow.validityChecker()) {
						String[] word = word_var.split("");
						if (MainWindow._window._pd.isValid().equals("VALID")) {
							JOptionPane.showMessageDialog(
									null,
									"The word "
											+ word_var
											+ " is"
											+ (MainWindow._window._pd
													.simulate(word) ? ""
													: " not")
											+ " in the language.", "Result!",
									JOptionPane.DEFAULT_OPTION);
						} else {
							JOptionPane.showMessageDialog(null,
									MainWindow._window._pd.isValid(), "Error!",
									JOptionPane.DEFAULT_OPTION);
						}
					}
				}
			});
		}
	}

	public class Save_Button extends JButton {
		public Save_Button(String buttonText) {
			super(buttonText);
			this.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					MainWindow.writeSaveFile();
				}
			});
		}
	}

	/*
	 * MAIN METHOD
	 */

	public static void main(String[] args) {
		MainWindow window = new MainWindow();
		MainWindow._window = window;
		// MainWindow.PDA_rb.addActionListener(window);
		MainWindow._window.open();
		Dimension screenRes = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		double width = screenRes.getWidth();
		double height = screenRes.getHeight() - 40;
		window.setSize((int) width, (int) height);
		DrawingView view = window.view();
		view.setBackground(new Color(255, 255, 255));
		ProxyDiagram pd = new ProxyDiagram();
		window.setPD(pd);
		pd.setMainWindow(window);
		if (MainWindow.initial) { // Run for the first time
			MainWindow.initial = false;
			// Choose default diagram below
			// Make sure to update the DFA/NFA/PDA status if you change this
			// A DFA:
			// String input =
			// "0,1;q1,q2,q3;q1;q1;q1+0=q1,q1+1=q2,q2+0=q3,q2+1=q1,q3+0=q2,q3+1=q3;";
			// http://i.imgur.com/2DXHK3Q.png
			// An NFA:
			String input = "0,1;1[220]270,2[420]145,3[420]395,4[620]270;1;3,4;1+0=2,1+=3,2+1=4,2+1=2,3+=2,3+0=4,4+0=3;";
			// http://i.imgur.com/xlauZGY.png
			// Another NFA:
			// String input =
			// "0,1;q0,q1,q2,q3,q4;q2,q4;q0+0=q0,q0+1=q0,q0+0=q3,q0+1=q1,q1+1=q2,q2+0=q2,q2+1=q2,q3+0=q4,q4+0=q4,q4+1=q4;";
			// http://i.imgur.com/ICKLRDZ.jpg
			// A PDA:
			// String input =
			// "0,1;p,q,r,s;p;r;p+0=p,p+0=p,p+=q,p+1=s,q+1=q,q+0=s,q+=r,r+0=s,r+1=s,s+0=s,s+1=s;Z+AZ,A+AA,+,+,A+,+,Z+Z,+,+,+,+;A,Z;Z;";
			// http://i.imgur.com/dYGicQl.png
			// Another PDA:
			// String input =
			// "(,),x;a,b,c,d,e,f;a;e;a+=b,a+(=f,a+)=f,b+=c,b+(=b,b+x=b,b+)=b,b+)=f,c+=d,c+(=f,c+)=f,c+=e,d+=b,d+(=f,d+)=f,e+(=f,e+)=f,f+(=f,f+)=f;+$,+,+,t+,+(,+x,(+t,+,t+,+,+,$+,x+,+,+,+,+,+,+;(,),$,x,t;;";
			// http://i.imgur.com/Qoj0NsE.png
			alpha_tbox.setText("0,1");
			word_tbox.setText("");
			setOfStates_tbox.setText("1,2,3,4");
			StartState_tbox.setText("1");
			AcceptStates_tbox.setText("3,4");
			Transitions_tbox.setText("1+0=2,1+=3,2+1=4,2+1=2,3+=2,3+0=4,4+0=3");
			MainWindow.NFA_rb.setSelected(true);
			NondeterministicFiniteAutomata defaultDiagram = new NondeterministicFiniteAutomata(
					input);
			defaultDiagram.setProxyDiagram(pd);
			pd.setActiveDiagram(defaultDiagram);
			defaultDiagram.draw();
		} else {
			alpha_tbox.setText(args[0]);
			word_tbox.setText(args[1]);
			setOfStates_tbox.setText(args[2]);
			StartState_tbox.setText(args[3]);
			AcceptStates_tbox.setText(args[4]);
			Transitions_tbox.setText(args[5]);
			String s1 = args[0] + ";" + args[2] + ";" + args[3] + ";" + args[4]
					+ ";" + args[5] + ";";
			if (args[6].equals("DFA")) {
				DeterministicFiniteAutomata diagram = new DeterministicFiniteAutomata(
						s1);
				diagram.setProxyDiagram(pd);
				pd.setActiveDiagram(diagram);
				diagram.draw();
				MainWindow.DFA_rb.setSelected(true);
			}
			if (args[6].equals("NFA")) {
				NondeterministicFiniteAutomata diagram = new NondeterministicFiniteAutomata(
						s1);
				diagram.setProxyDiagram(pd);
				pd.setActiveDiagram(diagram);
				diagram.draw();
				MainWindow.NFA_rb.setSelected(true);
			}
			if (args[6].equals("PDA")) {
				s1 += args[7] + ";";
				StackAlpha_tbox.setVisible(true);
				StackAlpha_tbox.setText(args[7]);
				PushdownAutomata diagram = new PushdownAutomata(s1);
				diagram.setProxyDiagram(pd);
				pd.setActiveDiagram(diagram);
				diagram.draw();
				MainWindow.PDA_rb.setSelected(true);
			}
		}
		alpha_var = alpha_tbox.getText();
		word_var = word_tbox.getText();
		setOfStates_var = setOfStates_tbox.getText();
		StartState_var = StartState_tbox.getText();
		AcceptStates_var = AcceptStates_tbox.getText();
		Transitions_var = Transitions_tbox.getText();
		if (!word_var.equals("")) {
			/*
			 * If word_var isn't blank, we got here because we wanted to
			 * simulate something. So simulate it!
			 */
			String[] word = word_var.split("");
			if (MainWindow._window._pd.isValid().equals("VALID")) {
				JOptionPane.showMessageDialog(null, "The word " + word_var
						+ " is"
						+ (MainWindow._window._pd.simulate(word) ? "" : " not")
						+ " in the language.", "Result!", JOptionPane.DEFAULT_OPTION);
			} else {
				JOptionPane.showMessageDialog(null,
						MainWindow._window._pd.isValid(), "Error!",
						JOptionPane.ERROR);
			}
		}
	}
}